import { defineStore } from 'pinia';
import tool from './tool'

export const useStore = defineStore('store', {
    state: () => {
        return {
            mousePosition: {
                x: 0,
                y: 0
            },
            dialog: {
                archive: {
                    addOrEdit: {
                        show: false,
                        data: {},
                        callBack: () => { },
                    }
                },
                model: {
                    show: false,
                    title: '',
                    windowTitle: '',
                    showCancel: true,
                    okCallBack: () => { },
                    cancelCallBack: () => { },
                },
                input: {
                    show: false,
                    title: '',
                    default: '',
                    check: () => { },
                    okCallBack: () => { },
                    error: ''
                },
                form: {
                    show: false,
                    title: '',
                    controls: [

                    ],
                    disabled: false,
                    form: {},
                    value: {},
                    showCancel: true,
                    okCallBack: () => { },
                    cancelCallBack: () => { },
                    changeCallBack: () => { },
                    readMode: true
                }
            },
            userData: undefined,
            dict: []
        };
    },
    actions: {
        updateMousePosition(e) {
            this.mousePosition = e
        },
        showDialog(objectName = '', operationName = '', data = {}, callBack = () => { }) {
            this.dialog[objectName][operationName].show = true
            this.dialog[objectName][operationName].data = data
            this.dialog[objectName][operationName].callBack = callBack
        },
        hideDialog(objectName = '', operationName = '') {
            this.dialog[objectName][operationName].show = false
        },
        updateUserData(data) {
            this.userData = data
        },
        showModal(title = '', okCallBack = () => { }, showCancel = true, cancelCallBack = () => { }) {
            this.dialog.model.windowTitle = ''
            this.dialog.model.title = title
            this.dialog.model.okCallBack = okCallBack
            this.dialog.model.showCancel = showCancel
            this.dialog.model.cancelCallBack = cancelCallBack
            this.dialog.model.show = true
        },
        hideModal() {
            this.dialog.model.show = false
        },
        showModal2(title = '', content = '', okCallBack = () => { }, showCancel = true, cancelCallBack = () => { }) {
            this.dialog.model.windowTitle = title
            this.dialog.model.title = content
            this.dialog.model.okCallBack = okCallBack
            this.dialog.model.showCancel = showCancel
            this.dialog.model.cancelCallBack = cancelCallBack
            this.dialog.model.show = true
        },
        hideModal2() {
            this.dialog.model.show = false
        },
        showInput(title = '', dft = '', okCallBack = () => { }, check = () => { }) {
            this.dialog.input.show = true
            this.dialog.input.default = dft
            this.dialog.input.title = title
            this.dialog.input.okCallBack = okCallBack
            this.dialog.input.check = check
            this.dialog.input.error = ''
        },
        hideInput() {
            this.dialog.input.show = false
        },
        showFormDialog(title = '', controls = [], form = {}, value = {}, disabled = false, okCallBack = () => { }, showCancel = false, cancelCallBack = () => { }, changeCallBack = () => { }, readMode = false) {
            this.dialog.form.show = true
            this.dialog.form.title = title
            this.dialog.form.disabled = disabled
            this.dialog.form.controls = controls
            this.dialog.form.form = form
            this.dialog.form.value = value
            this.dialog.form.readMode = readMode
            this.dialog.form.okCallBack = okCallBack
            this.dialog.form.showCancel = showCancel
            this.dialog.form.cancelCallBack = cancelCallBack
            this.dialog.form.changeCallBack = changeCallBack
        },
        hideFormDialog() {
            this.dialog.form.show = false
        },
        setDict(dict) {
            this.dict = dict
        },
        searchDictByField(field) {
            for (let i = 0; i < this.dict.length; i++) {
                if (this.dict[i].field == field) return this.dict[i]
            }
        },
        searchDictByTitle(title) {
            for (let i = 0; i < this.dict.length; i++) {
                if (this.dict[i].title == title) return this.dict[i]
            }
        },
        searchDictByFieldArr(fieldArr) {
            return tool.deepCopyObject(this.dict).filter((item) => {
                return fieldArr.indexOf(item.field) + 1
            }).sort((a, b) => {
                return fieldArr.indexOf(a.field) - fieldArr.indexOf(b.field)
            })
        },
        setUserData(userData) {
            this.userData = userData
        },
        getUserData() {
            return this.userData
        },
        checkUserPression(permissionName) {
            for (let i = 0; i < this.userData.permissions.length; i++) {
                if (this.userData.permissions[i].name === permissionName) return this.userData.permissions[i]
            }
            return null
        }
    },
});